<template>
  <div>
    <div class="comment-item" v-for="item in commentList"
      :key="item.id">
      <div class="comment-header">
        <div class="user-info" @click="viewUser(item.userId)">
          <el-avatar :size="30" style="margin-right: 10px;">
            <div style="background-color: #00caa8; border-radius: 50%;">
              {{ item.username.slice(0, 1) }}
            </div>
          </el-avatar>
          <div>{{ item.username }}</div>
        </div>
        <el-rate v-model="item.rating" disabled />
      </div>
      <div class="comment-content">
        {{ item.content }}
      </div>
      <div class="comment-time">
        {{ item.createTime ? item.createTime.replace('T', ' ') : '' }}
      </div>
    </div>
    <el-empty v-if="commentList.length === 0"
      description="暂无数据"></el-empty> 
  </div>
</template>

<script>
  export default {
    name: 'CommentList',
    props: {
      commentList: {
        type: Array,
        default: []
      }
    },
    methods: {
      viewUser(userId) {
        const id = String(userId).slice(3)
        this.$router.push(`/userCenter/${id}`);
      }
    }
  }
</script>

<style scoped lang="scss">
.comment-item {
  text-align: left;
  padding: 20px 10px;
  .comment-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-info {
      cursor: pointer;
      display: flex;
      align-items: center;
    }
  }
  .comment-content {
    margin: 10px 0;
  }
  .comment-time {
    font-size: 0.9em;
    color: #a29b9d;
  }
}

.comment-item:not(:last-child) {
  border-bottom: 1px solid #eaeaea;
}
</style>